package com.dailyblue.example.springcloud.sentinel.consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dailyblue.example.springcloud.commons.util.JsonResult;
import com.dailyblue.example.springcloud.sentinel.consumer.service.SentinelConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/sentinel_consumer")
public class SentinelConsumerController {
    @Resource
    private SentinelConsumerService service;

    @GetMapping("/a")
    public JsonResult a() {
        return service.a();
    }

    @GetMapping("/b")
    public JsonResult b() {
        return service.b();
    }

    @GetMapping("/c")
    public JsonResult c() {
        return service.c();
    }

    @GetMapping("/d1")
    public JsonResult d1() {
        return service.d();
    }

    @GetMapping("/d2")
    public JsonResult d2() {
        return service.d();
    }

    @GetMapping("/e")
    public JsonResult e() {
        int num = (int) (Math.random() * 3);
        int result = 3 / num;
        System.out.println(result);
        return service.c();
    }

    @GetMapping("/f")
    @SentinelResource("messageF")
    public JsonResult f(int a, int b) {
        log.info("a:{},b:{}", a, b);
        return service.c();
    }

    @GetMapping("/g/{index}")
    public JsonResult g(@PathVariable int index) {
        return service.g(index);
    }

    @GetMapping("/h/{index}")
    public JsonResult h(@PathVariable int index) {
        return service.h(index);
    }
}
